package com.rock.code.bigdata.targetstop.service.impl;

import com.github.pagehelper.PageInfo;
import com.rock.code.base.service.impl.BaseServiceImpl;
import com.rock.code.bigdata.forecast.dao.ForecastDao;
import com.rock.code.bigdata.forecast.service.ForeCastService;
import com.rock.code.bigdata.targetstop.dao.TargetsTopDao;
import com.rock.code.bigdata.targetstop.service.TargetsTopService;
import com.rock.code.common.pager.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class TargetsTopServiceImpl implements TargetsTopService {

    @Resource
    private TargetsTopDao targetsTopDao;

    @Override
    public List targetsTop() {
        return targetsTopDao.targetsTop();
    }

    @Override
    public String monthTableName() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        Date time = cal.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        String month = sdf.format(time);
        return "tb_kpi_value_month_" + month;
        }

    @Override
    public String day() {
        return null;
    }

    @Override
    public List queryData(Map map) {
        return targetsTopDao.queryData(map);
    }

    @Override
    public List queryDataYear(Map map) {
        return targetsTopDao.queryDataYear(map);
    }


    @Override
    public List queryDataMonth(Map map) {
        return targetsTopDao.queryDataMonth(map);
    }

    @Override
    public String monthYearTableName() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.MONTH, -1);
        String year = new SimpleDateFormat("yyyy").format(cal.getTime());
        return "tb_kpi_value_year_" + year;
    }

    @Override
    public String lastMonth() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.MONTH, -1);
        String month = new SimpleDateFormat("yyyy-MM").format(cal.getTime());
        return month;
    }

    @Override
    public String yearTableName() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.MONTH, -1);
        String year = new SimpleDateFormat("yyyy").format(cal.getTime());
        return "tb_kpi_value_year_" + year;
    }

    @Override
    public String weekTableName() {
        Calendar cal = Calendar.getInstance();
        String name = "";
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        String lastweek = String.valueOf(cal.get(Calendar.WEEK_OF_YEAR) - 1);
        String weeknum = String.valueOf(cal.getActualMaximum(Calendar.WEEK_OF_YEAR));//52
        if (lastweek.equals(weeknum)) {
            cal.add(Calendar.YEAR, -1);
            String lastmonthtable = new SimpleDateFormat("yyyy").format(cal.getTime());
            name = "tb_kpi_value_week_" + lastmonthtable;//2017
        } else {
            name = "tb_kpi_value_week_" + cal.get(Calendar.YEAR);//2018
        }
        return name;
    }

    @Override
    public String lastWeek() {
        Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        String lastWeek = String.valueOf(cal.get(Calendar.WEEK_OF_YEAR) - 1);
        System.out.println("上周是这年的第" + lastWeek + "周");
        return lastWeek;
    }

    @Override
    public List queryDateByWeek(Map map) {
        return targetsTopDao.queryDateByWeek(map);
    }

    @Override
    public List queryLineByDate(HashMap map) {
        return targetsTopDao.queryLineByDate(map);
    }
}
